package ext.tianma.version;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import wt.doc.WTDocument;
import wt.fc.Persistable;
import wt.fc.collections.WTCollection;
import wt.lifecycle.LifeCycleManaged;
import wt.part.WTPart;
import wt.part.WTProductInstance2;
import wt.session.SessionHelper;
import wt.util.WTException;

import com.ptc.core.components.forms.FormProcessingStatus;
import com.ptc.core.components.forms.FormResult;
import com.ptc.core.ui.resources.FeedbackType;
import com.ptc.netmarkets.util.beans.NmCommandBean;
import com.ptc.netmarkets.util.misc.NmContext;
import com.ptc.windchill.enterprise.part.forms.PartDocRelationDefaultObjectFormProcessor;
import com.ptc.windchill.enterprise.util.PartManagementHelper;


/**Description:【部件】关联【参考文档】
 * @author    :  YangWen
 * @create at :  2013-8-30   上午11:15:14
 */
public class PartDocRefRelationProcessor extends PartDocRelationDefaultObjectFormProcessor {

    public PartDocRefRelationProcessor() {
    	
    }

    public FormResult doOperation(NmCommandBean nmcommandbean, List list) throws WTException {
    	  
        wt.fc.Persistable persistable = nmcommandbean.getPrimaryOid().getWtRef().getObject();       
        FormResult formresult = new FormResult();
        formresult.setStatus(FormProcessingStatus.SUCCESS);
        
        if(persistable instanceof WTPart)  {
            WTPart part = (WTPart)persistable;   
            
            if (part instanceof LifeCycleManaged) {
			    LifeCycleManaged lcm = (LifeCycleManaged) part;
			    String lifecycleState = lcm.getLifeCycleState().toString();
			    System.out.println("当前对象"+part.getDisplayIdentifier()+"的生命周期状态是："+lifecycleState);
			  	   	    	
		    	 ArrayList<?> selectedList = nmcommandbean.getSelected();            
		            if(selectedList == null || selectedList.size() == 0)
		            	return null;			            
		            for(int i=0 ; i<selectedList.size();++i){
			            NmContext nmcontext = (NmContext) selectedList.get(i);	            
			            Persistable obj = nmcontext.getTargetOid().getWtRef().getObject();	            
			            if(obj instanceof WTDocument)
			            	PartRefDocumentUtil.createRelation(part, (WTDocument)obj);				            
		           }			    	
			    
			}else{
				//formresult = PartDocRefRelationFormProcessor(nmcommandbean , list);
				return null;
			}
           
       } 
        
        return formresult;
    }
    
    
    
    
	
	
	public FormResult PartDocRefRelationFormProcessor(NmCommandBean nmcommandbean, List list)
	        throws WTException
	    {
	        Object obj = null;
	        Vector vector = new Vector();
	        wt.fc.Persistable persistable = nmcommandbean.getPrimaryOid().getWtRef().getObject();
	        String s = "relatedPartsReferences";
	        FormResult formresult = new FormResult();
	        formresult.setStatus(FormProcessingStatus.SUCCESS);
	        if(persistable instanceof WTPart)
	        {
	            WTPart wtpart = (WTPart)persistable;
	            WTCollection wtcollection = PartManagementHelper.addRelation(nmcommandbean, true);
	            WTPart wtpart1 = wtpart;
	            for(Iterator iterator = wtcollection.persistableIterator(); iterator.hasNext();)
	            {
	                Object obj1 = iterator.next();
	                if(obj1 instanceof WTPart)
	                    wtpart1 = (WTPart)obj1;
	                else
	                    vector.add(obj1);
	            }

	            if(wtpart.equals(wtpart1))
	                redirectURL = null;
	            else
	                redirectURL = getURL(wtpart1, s);
	        } else
	        if(persistable instanceof WTProductInstance2)
	        {
	            WTProductInstance2 wtproductinstance2 = (WTProductInstance2)persistable;
	            WTCollection wtcollection1 = PartManagementHelper.addRelation(nmcommandbean, true);
	            WTProductInstance2 wtproductinstance2_1 = wtproductinstance2;
	            for(Iterator iterator1 = wtcollection1.persistableIterator(); iterator1.hasNext();)
	            {
	                Object obj2 = iterator1.next();
	                if(obj2 instanceof WTProductInstance2)
	                    wtproductinstance2_1 = (WTProductInstance2)obj2;
	                else
	                    vector.add(obj2);
	            }

	            if(wtproductinstance2.equals(wtproductinstance2_1))
	                redirectURL = null;
	            else
	                redirectURL = getURL(wtproductinstance2_1, s);
	        }
	        if(vector.size() != 0)
	        {
	            java.util.Locale locale = SessionHelper.getLocale();
	            formresult.addFeedbackMessage(PartManagementHelper.getAddFbMsg(FeedbackType.FAILURE, vector, locale));
	            formresult.setStatus(FormProcessingStatus.SUCCESS);
	        }
	        super.doOperation(nmcommandbean, list);
	        return formresult;
	    }
	
    
    
    
}

